package com.moxiao.yfjh.module.blind.controller.admin.background;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.*;
import com.moxiao.yfjh.module.blind.convert.background.BackgroundConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.background.BackgroundDO;
import com.moxiao.yfjh.module.blind.service.background.BackgroundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 背景图管理")
@RestController
@RequestMapping("/blind/background")
@Validated
public class BackgroundController {

    @Resource
    private BackgroundService backgroundService;

    @PostMapping("/create")
    @Operation(summary = "创建背景图管理")
    @PreAuthorize("@ss.hasPermission('blind:background:create')")
    public CommonResult<Long> createBackground(@Valid @RequestBody BackgroundCreateReqVO createReqVO) {
        return success(backgroundService.createBackground(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新背景图管理")
    @PreAuthorize("@ss.hasPermission('blind:background:update')")
    public CommonResult<Boolean> updateBackground(@Valid @RequestBody BackgroundUpdateReqVO updateReqVO) {
        backgroundService.updateBackground(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除背景图管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:background:delete')")
    public CommonResult<Boolean> deleteBackground(@RequestParam("id") Long id) {
        backgroundService.deleteBackground(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得背景图管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:background:query')")
    public CommonResult<BackgroundRespVO> getBackground(@RequestParam("id") Long id) {
        BackgroundDO background = backgroundService.getBackground(id);
        return success(BackgroundConvert.INSTANCE.convert(background));
    }

    @GetMapping("/list")
    @Operation(summary = "获得背景图管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:background:query')")
    public CommonResult<List<BackgroundRespVO>> getBackgroundList(@RequestParam("ids") Collection<Long> ids) {
        List<BackgroundDO> list = backgroundService.getBackgroundList(ids);
        return success(BackgroundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得背景图管理分页")
    @PreAuthorize("@ss.hasPermission('blind:background:query')")
    public CommonResult<PageResult<BackgroundRespVO>> getBackgroundPage(@Valid BackgroundPageReqVO pageVO) {
        PageResult<BackgroundDO> pageResult = backgroundService.getBackgroundPage(pageVO);
        return success(BackgroundConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出背景图管理 Excel")
    @PreAuthorize("@ss.hasPermission('blind:background:export')")
    @OperateLog(type = EXPORT)
    public void exportBackgroundExcel(@Valid BackgroundExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<BackgroundDO> list = backgroundService.getBackgroundList(exportReqVO);
        // 导出 Excel
        List<BackgroundExcelVO> datas = BackgroundConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "背景图管理.xls", "数据", BackgroundExcelVO.class, datas);
    }

}
